BEGIN;
CREATE TABLE "KLADR_ABBREVIATION" (
    "ID" integer NOT NULL PRIMARY KEY,
    "LEVEL" integer NOT NULL,
    "DISPOSITION" integer NOT NULL,
    "NAME" varchar(40) NOT NULL,
    "SHORT_NAME" varchar(10) NOT NULL
)
;
CREATE GENERATOR "KLADR_ABBREVIATION_GN"

            CREATE TRIGGER "KLADR_ABBREVIATION_TR" FOR "KLADR_ABBREVIATION"
            BEFORE INSERT
            AS 
            BEGIN
               IF (NEW."ID" IS NULL) THEN 
                   NEW."ID" = GEN_ID("KLADR_ABBREVIATION_GN", 1);
            END
CREATE TABLE "KLADR_STATUS" (
    "ID" integer NOT NULL PRIMARY KEY,
    "NAME" varchar(125) NOT NULL
)
;
CREATE GENERATOR "KLADR_STATUS_GN"

            CREATE TRIGGER "KLADR_STATUS_TR" FOR "KLADR_STATUS"
            BEFORE INSERT
            AS 
            BEGIN
               IF (NEW."ID" IS NULL) THEN 
                   NEW."ID" = GEN_ID("KLADR_STATUS_GN", 1);
            END
CREATE TABLE "KLADR_ADDRESS" (
    "ID" integer NOT NULL PRIMARY KEY,
    "PARENT_ID" integer,
    "NAME" varchar(40) NOT NULL,
    "ABBREV_ID" integer NOT NULL REFERENCES "KLADR_ABBREVIATION" ("ID"),
    "COUNTRY_CODE" integer,
    "REGION_CODE" integer,
    "AREA_CODE" integer,
    "CITY_CODE" integer,
    "ITEM_CODE" integer,
    "URGECY_CODE" integer,
    "STREET_CODE" integer,
    "ZIP_CODE" integer,
    "GNIMB" integer,
    "UNO" integer,
    "OCATD" integer,
    "KL_STATUS_ID" integer NOT NULL REFERENCES "KLADR_STATUS" ("ID"),
    "IS_KLADR" integer NOT NULL,
    "NOTE" blob sub_type 1,
    UNIQUE ("COUNTRY_CODE", "REGION_CODE", "AREA_CODE", "CITY_CODE", "ITEM_CODE", "URGECY_CODE", "STREET_CODE")
)
;
CREATE GENERATOR "KLADR_ADDRESS_GN"

            CREATE TRIGGER "KLADR_ADDRESS_TR" FOR "KLADR_ADDRESS"
            BEFORE INSERT
            AS 
            BEGIN
               IF (NEW."ID" IS NULL) THEN 
                   NEW."ID" = GEN_ID("KLADR_ADDRESS_GN", 1);
            END
ALTER TABLE "KLADR_ADDRESS" ADD CONSTRAINT "PARENT_ID_REFS_ID_72CD1E75" FOREIGN KEY ("PARENT_ID") REFERENCES "KLADR_ADDRESS" ("ID");
CREATE INDEX "KLADR_ADDRESS_PARENT_ID" ON "KLADR_ADDRESS" ("PARENT_ID");
CREATE INDEX "KLADR_ADDRESS_ABBREV_ID" ON "KLADR_ADDRESS" ("ABBREV_ID");
CREATE INDEX "KLADR_ADDRESS_KL_STATUS_ID" ON "KLADR_ADDRESS" ("KL_STATUS_ID");
COMMIT;
